package hu.uszeged.inf.stud.prf.todo.bean.usertask;

import hu.uszeged.inf.stud.prf.todo.dao.task.TaskTypeDAO;
import hu.uszeged.inf.stud.prf.todo.dao.usertask.UserTaskDAO;
import hu.uszeged.inf.stud.prf.todo.entity.task.TaskType;
import hu.uszeged.inf.stud.prf.todo.entity.user.User;import hu.uszeged.inf.stud.prf.todo.entity.usertask.UserTask;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

@ManagedBean
@ViewScoped
public class UserTaskListBean {
	
	@Inject
	private UserTaskDAO userTaskDAO;
	
	@Inject
	private TaskTypeDAO taskTypeDAO;
		
	private Long taskTypeId;
	
	private List<UserTask> usertasks;
	
	public void load(User user) {
		if (null == taskTypeId || taskTypeId == 0L) {
			usertasks = getUserTasksForUser(user);
		} else {
			usertasks = getUserTasksForTaskTypeId(user);
			for(int i=0; i<usertasks.size();) {
				if(usertasks.get(0).isArchive()) {
					usertasks.remove(i);
				} else {
					++i;
				}
			}
		}
	}

	public List<UserTask> getUserTasksForUser(User user) {
		return userTaskDAO.getUserTasksForUser(user);
	}
	
	public List<UserTask> getUserTasksForTaskTypeId(User user) {
		TaskType tt = taskTypeDAO.findById(taskTypeId, false);
		List<UserTask> userList = new ArrayList<UserTask>();
		userList = tt.getUsertasks();
		for(int i=0; i<userList.size();) {
			if(!userList.get(0).getUser().equals(user)) {
				userList.remove(i);
			} else {
				++i;
			}
		}
		return userList;
	}
	
	public Long getTaskTypeId() {
		return taskTypeId;
	}

	public void setTaskTypeId(Long taskTypeId) {
		this.taskTypeId = taskTypeId;
	}

	public List<UserTask> getUsertasks() {
		return usertasks;
	}

	public void setUsertasks(List<UserTask> usertasks) {
		this.usertasks = usertasks;
	}

	public int getUserTodoCount(User user) {
		if (taskTypeId!=null && taskTypeId != 0) {
			TaskType tt = taskTypeDAO.findById(taskTypeId, false);
			int cnt = 0;
			for(UserTask ut : usertasks) {
				if (ut.getTasktypes().contains(tt) && !ut.getState()) {
					++cnt;
				}
			}
			return cnt;
		} else {
			return userTaskDAO.getUserTodoCount(user);
		}
	}
	
	public int getUserDoneCount(User user) {
		if (taskTypeId != null && taskTypeId != 0) {
			TaskType tt = taskTypeDAO.findById(taskTypeId, false);
			int cnt = 0;
			for(UserTask ut : usertasks) {
				if (ut.getTasktypes().contains(tt) && ut.getState()) {
					++cnt;
				}
			}
			return cnt;
		} else {
			return userTaskDAO.getUserDoneCount(user);
		}
	}
	
	public int getCompletedPercent(User user){
		int done = getUserDoneCount(user);
		int total = done +getUserTodoCount(user);
		if(total==0){
			return 100;
		}
		return (done*100)/total;
	}
	
	public List<UserTask> loadAllUserTasks() {
		return userTaskDAO.findByCriteria();	}
	
	public List<TaskType> loadTaskTypes(User user) {
		return taskTypeDAO.getTaskTypesForUser(user);
	}

}
